package com.example.backend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.entity.Post;
import com.example.backend.entity.PostVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Author Jcy
 * @Date 2022/5/4
 */
@Mapper
public interface PostMapper extends BaseMapper<Post> {
    /**
     * @param page 翻页对象
     * @return
     */
    @Select("SELECT DISTINCT tb_post.post_id AS postId,tb_post.post_content AS content, (SELECT tb_user.nickname from tb_user WHERE tb_user.user_id = (SELECT tb_user_post.user_id FROM tb_user_post WHERE tb_user_post.post_id = postID)) AS author,(SELECT COUNT(tb_like.like_user_id) FROM tb_like WHERE tb_like.like_post_id = postId) AS likecount, (SELECT COUNT(tb_comment.comment_id) FROM tb_comment WHERE tb_comment.comment_post_id = postId) AS commentcount FROM tb_post,tb_user,tb_user_post,tb_like,tb_comment")
    List<PostVO> getPost(Page<PostVO> page);
    @Select("SELECT DISTINCT tb_post.post_id AS postId,tb_post.post_content AS content, (SELECT tb_user.nickname from tb_user WHERE tb_user.user_id =#{userId}) AS author,(SELECT COUNT(tb_like.like_id) FROM tb_like WHERE tb_like.like_post_id = postId) AS likecount, (SELECT COUNT(tb_comment.comment_id) FROM tb_comment WHERE tb_comment.comment_post_id = postId) AS commentcount FROM tb_post,tb_user,tb_user_post,tb_like,tb_comment WHERE tb_user_post.user_id =#{userId}")
    List<PostVO> getUserPost(@Param("userId") int userId, Page<PostVO> page);
}
